//
//  TF_LivingNetworkSelection.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/2.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_LivingNetworkSelection: TF_TopRightOrCornerTopLeft {

    let label = UILabel()
    let line = UIView()
    let content = UIView()
    let listView = UIScrollView()
    let list = NSMutableArray()
    override init(frame: CGRect) {
        super.init(frame: frame)
      

        self.label.textAlignment = .center;
        self.label.numberOfLines = 0;
        self.addSubview(self.label)
        
       

        self.label.attributedText = NSMutableAttributedString.getAttributed(string: "网络选择", font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 35/255.0, green: 57/255.0, blue: 64/255.0, alpha: 1.0));
        self.label.textColor = UIColor(red: 35/255.0, green: 57/255.0, blue: 64/255.0, alpha: 1.0)


     //线
     self.line.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 239/255.0, alpha: 1.0)
    self.addSubview(self.line)

    self.content.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
    self.addSubview(self.content)
        

    self.listView.backgroundColor = UIColor(red: 235/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

    self.content.addSubview(self.listView)
     
    }
    @objc func closeClick()
    {

    }
    override func layoutSubviews() {
        super.layoutSubviews()
       
        self.label.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 50)
        self.line.frame = CGRect(x: 0, y: 49.5, width: self.frame.size.width, height: 0.5)

        self.content.frame = CGRect(x: 0, y: self.frame.size.height - 110, width: self.frame.size.width, height: 110)
        self.initMask()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc public func refreshData(array:NSArray,target:NSObject,action:Selector ,controlEvents:UIControl.Event)
  
    {
       
        if (self.listView.subviews.count==2&&array.count>0) {
            //取第0组的
             let groupIndex:NSInteger = 0;
             let data =  array[groupIndex] as!TFLiveLineData
            
            
            var listViewWidth:CGFloat = CGFloat(data.lineList.count * 85)
            let listViewHight:CGFloat = 35
            let listViewY:CGFloat = (self.content.frame.size.height-listViewHight)/2
            var listViewX:CGFloat = (self.content.frame.size.width-listViewWidth)/2
            
            self.listView.contentSize = CGSize(width: listViewWidth, height: listViewHight)
         
            if (listViewWidth > self.frame.size.width) {
                listViewWidth = self.frame.size.width;
                listViewX = 0
            }
            self.listView.frame = CGRect(x: listViewX, y: listViewY, width: listViewWidth, height: listViewHight)
        
            
            
            self.listView.layer.cornerRadius = listViewHight / 2
            self.list.removeAllObjects()
            // 线路列表出来
            for j in stride(from: 0, to: data.lineList.count ,by: 1) {
         
         
                
                let btn = TF_LivingButton()
                    btn.groupIndex = groupIndex
                    btn.tag = j
                    btn.networkDict = data.lineList[j] as! TFLiveLine
                    let btnWidth:CGFloat = 85
                    let btnX:CGFloat = CGFloat(j) * btnWidth;
                    btn.frame = CGRect(x: btnX, y: 0, width: btnWidth, height: 35)
                 
            
                   
                    if (j==0) {
                        btn.backgroundColor = UIColor(red: 1/255.0, green: 194/255.0, blue: 255/255.0, alpha: 1.0)
              
                    }else{
                        btn.backgroundColor = UIColor.clear
                    }
                
                     btn.titleLabel!.font = UIFont.PingFang(size: 16)
                     btn.setTitle("线路"+(NSString.init(format: "%i", j+1) as String), for: .normal)
            
                    btn.layer.cornerRadius = 17.5
               
                   btn.addTarget(target, action: action, for: controlEvents)
                self.listView.addSubview(btn)
                self.list.add(btn)
                }

        
        }
    }
    
    @objc public func networkSelectionClick(button:TF_LivingButton)
  
    {
        for i in stride(from: 0, to: self.list.count ,by: 1) {
            let btn = self.list[i] as! TF_LivingButton
            
            btn.backgroundColor = UIColor.clear

          }
        button.backgroundColor = UIColor(red: 1/255.0, green: 194/255.0, blue: 255/255.0, alpha: 1.0)
        

   }
    
}
